package com.spring.appservice.web.controller;

import java.util.Map;

import javax.annotation.Resource;

import com.fasterxml.jackson.databind.util.JSONPObject;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Maps;
import com.mangofactory.swagger.annotations.ApiIgnore;
import com.spring.appservice.web.service.MainShowService;
import com.spring.security.core.util.ApplicationUtils;

@RestController
@RequestMapping(value = "/family")
@ApiIgnore
public class FamilyController {

	@Resource
	private MainShowService mainShowService;

	/**
	 * 获取请信息
	 * 
	 * @param token
	 * @param callback
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/invite")
	public Object invite(String token,String roleId,String callback) throws Exception {
		Map<String, Object> rt = Maps.newHashMap();
		try {
			rt = mainShowService.createInviteCode(token,roleId);
		} catch (Exception e) {
			rt.put(ApplicationUtils.STATUS, "0");
			rt.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();
		}
		if (ApplicationUtils.isNotNull(callback)) {
			return new JSONPObject(callback, rt);
		} else {
			return rt;
		}
	}

	/**
	 * 接受邀请
	 * @param token
	 * @param inviteCode
	 * @param callback
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/acceptInvite")
	public Object acceptInvite(String token, String inviteCode, String callback)
			throws Exception {
		Map<String, Object> rt = Maps.newHashMap();
		try {
			rt = mainShowService.acceptInvite(token, inviteCode);
		} catch (Exception e) {
			rt.put(ApplicationUtils.STATUS, "0");
			rt.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();
		}
		if (ApplicationUtils.isNotNull(callback)) {
			return new JSONPObject(callback, rt);
		} else {
			return rt;
		}
	}
	
	/**
	 * 获取当前用户未使用过的邀请码
	 * @param token
	 * @param callback
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/UnUsedInvites",method = RequestMethod.GET)
	public Object UnUsedInvites(String token, String callback)
			throws Exception {
		Map<String, Object> rt = Maps.newHashMap();
		try {
			rt = mainShowService.UnUsedInvites(token);
		} catch (Exception e) {
			rt.put(ApplicationUtils.STATUS, "0");
			rt.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();
		}
		if (ApplicationUtils.isNotNull(callback)) {
			return new JSONPObject(callback, rt);
		} else {
			return rt;
		}
	}
}
